// components/area-checkbox.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    areaList:[],
    selectedAreas: [],
    mainActiveIndex: 0,
    activeId: [],
    max: 2
  },

  lifetimes:{
    attached:function(){
      this.loadAreaData();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    onClickNav({ detail = {} }) {
      console.log(detail);
      this.setData({
        mainActiveIndex: detail.index || 0,
      });
      console.log("onClickNav:"+this.data.mainActiveIndex);
    },
  
    onClickItem({ detail = {} }) {
      console.log(detail);
      const { activeId } = this.data;
      const index = activeId.indexOf(detail.id);
      if (index > -1) {
        activeId.splice(index, 1);
      } else {
        activeId.push(detail.id);
      }
  
      this.setData({ activeId });
      console.log("onClickNav:"+this.data.activeId);
    },

    selectArea(event) {
      this.setData({
        selectedAreas: event.detail,
      });
      const comSelectedAreas = this.data.selectedAreas;      
      // 向父页面传递数据
      this.triggerEvent('selectArea', {
        comSelectedAreas: comSelectedAreas
      });
      // console.log(event.detail);
    },

    loadAreaData: function(){
      this.setData({
        areaList: [{text:'海珠区',badge:7,dot:true,disabled:false,children:[{text:'康乐',id:1,disabled:false,},{text:'鹭江',id:2,},{text:'大塘',id:3,},{text:'赤沙',id:4,},{text:'赤岗',id:5,},{text:'龙潭',id:6,},{text:'土华',id:7,}],},{text:'天河区',badge:2,dot:true,disabled:false,children:[{text:'大源',id:1,disabled:false,},{text:'太和',id:2,}],},{text:'番禺区',badge:11,dot:true,disabled:false,children:[{text:'南村',id:1,disabled:false,},{text:'板桥',id:2,},{text:'员岗',id:3,},{text:'塘步东',id:4,},{text:'塘步西',id:5,},{text:'河村',id:6,},{text:'植村',id:7,},{text:'礼村',id:8,},{text:'大山',id:9,},{text:'东联',id:10,},{text:'猛涌',id:11}]}]
      });
      // 将客户端缓存中的选中区域，回填到selectedAreas
      try {
        this.setData({
          selectedAreas: wx.getStorageSync('selectedAreas') || [],
        });
      } catch (e) {
        console.error('读取selectedAreas缓存失败', e);
      }
    },
  }
})